﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TanWeb.Models.Entities;
using TanWeb.Helpers;
using TanWeb.Models.DataAccess;
using TanWeb.Controllers.Business.Infrastructure;

namespace TanWeb.Controllers.Business
{
    public class DetailBusiness : BaseBusiness
    {
        public List<MenuItem> GetMenuItems()
        {
            logger.Info("\tStart DetailBusiness - GetMenuItems");
            List<MenuItem> menuItems = new List<MenuItem>();
            ItemGroupDataAccess groupAccess = new ItemGroupDataAccess(this.DbContext);
            SubItemGroupDataAccess subGroupAccess = new SubItemGroupDataAccess(this.DbContext);

            try
            {
                List<ItemGroup> itemGroups = groupAccess.GetAllItemGroup();

                foreach (ItemGroup itemGroup in itemGroups)
                {
                    List<SubItemGroup> subItemGroups = subGroupAccess.GetSubItemsByGroupId(itemGroup.id);
                    MenuItem mnuItem = new MenuItem(itemGroup, subItemGroups);
                    menuItems.Add(mnuItem);
                }
            }
            catch (Exception ex)
            {
                logger.Error("\tError - HomeBusiness - GetMenuItems", ex);
                throw ex;
            }

            logger.Info("\tEnd DetailBusiness - GetMenuItems");
            return menuItems;
        }

        public List<ProductProfile> GetProductInfo(string groupId = null, string subGroupId = null, int pageIndex = 1)
        {
            logger.Info("\tStart DetailBusiness - GetProductInfo");
            ProductDataAccess access = new ProductDataAccess(this.DbContext);
            List<ProductProfile> productProfile = new List<ProductProfile>();
            
            try
            {
                productProfile = access.GetProductInfo(groupId, subGroupId, pageIndex);
            }
            catch (Exception ex)
            {
                logger.Error("\tError DetailBusiness - GetProductInfo: ", ex);
                throw ex;
            }

            logger.Info("\tEnd DetailBusiness - GetProductInfo");
            return productProfile;
        }

        public int GetAllPageRecord(string groupId = null, string subGroupId = null)
        {
            logger.Info("\tStart DetailBusiness - GetAllPageRecord");

            int productCount = 0;
            ProductDataAccess access = new ProductDataAccess(this.DbContext);

            try
            {
                productCount = access.CountProductInfo(groupId, subGroupId);
            }
            catch (Exception ex)
            {
                logger.Error("\tError DetailBusiness - GetAllPageRecord: ", ex);
                throw ex;
            }

            logger.Info("\tEnd DetailBusiness - GetAllPageRecord");

            return productCount / Constants.MAX_PAGE_RECORD + 
                (productCount % Constants.MAX_PAGE_RECORD == 0 ? 0 : 1);
        }

        public string[] GetNameInfo(string groupId = null, string subGroupId = null)
        {
            logger.Info("\tStart DetailBusiness - GetNameInfo");
            string nameInfo = string.Empty;
            ProductDataAccess access = new ProductDataAccess(this.DbContext);

            try
            {
                if (groupId != null || subGroupId != null)
                {
                    groupId = groupId == null ? string.Empty : groupId;
                    subGroupId = subGroupId == null ? string.Empty : subGroupId;

                    nameInfo = access.GetNameInfo(groupId, subGroupId);

                    if (nameInfo == null)
                    {
                        nameInfo = Constants.SPECIAL_SPACE_CHAR;
                    }

                    return System.Text.RegularExpressions.Regex.Split(nameInfo, Constants.SPECIAL_SPACE_CHAR);
                }
            }
            catch (Exception ex)
            {
                logger.Error("\tError DetailBusiness - GetNameInfo: ", ex);
                throw ex;
            }

            logger.Info("\tEnd DetailBusiness - GetNameInfo");

            return null;
        }

        public Product GetProductInfo(string groupId, string subGroupId, string productId)
        {
            logger.Info("\tStart DetailBusiness - GetProductInfo");
            Product productInfo = null;
            ProductDataAccess access = new ProductDataAccess(this.DbContext);
            groupId = groupId == null ? string.Empty : groupId;
            subGroupId = subGroupId == null ? string.Empty : subGroupId;
            productId = productId == null ? string.Empty : productId;

            try
            {
                if (groupId != string.Empty && subGroupId != string.Empty && productId != string.Empty)
                {
                    productInfo = access.GetProductInfo(groupId, subGroupId, productId);
                }
            }
            catch (Exception ex)
            {
                logger.Error("\tError DetailBusiness - GetProductInfo: ", ex);
                throw ex;
            }

            logger.Info("\tEnd DetailBusiness - GetProductInfo");

            return productInfo;
        }

        public int UpdateViewCount(string groupId, string subGroupId, string productId)
        {
            logger.Info("\tStart DetailBusiness - GetProductInfo");
            int result = 0;
            ProductDataAccess access = new ProductDataAccess(this.DbContext);
            groupId = groupId == null ? string.Empty : groupId;
            subGroupId = subGroupId == null ? string.Empty : subGroupId;
            productId = productId == null ? string.Empty : productId;

            try
            {
                if (groupId != string.Empty && subGroupId != string.Empty && productId != string.Empty)
                {
                    result = access.UpdateViewCount(groupId, subGroupId, productId);
                }
                else
                {
                    result = Constants.ERROR_SQL_CONDITION;
                }
            }
            catch (Exception ex)
            {
                logger.Error("\tError DetailBusiness - GetProductInfo: ", ex);
                throw ex;
            }

            logger.Info("\tEnd DetailBusiness - GetProductInfo");

            return result;
        }

        public List<ItemGroup> GetAllItemGroup()
        {
            logger.Info("\tStart Business: Master; Function: GetAllItemGroup");

            ItemGroupDataAccess access = new ItemGroupDataAccess(this.DbContext);
            List<ItemGroup> group = new List<ItemGroup>();

            try
            {
                group = access.GetAllItemGroup();
            }
            catch (Exception ex)
            {
                logger.Error("\tError Business: Master; Function: GetAllItemGroup", ex);
                throw ex;
            }

            logger.Info("\tEnd Business: Master; Function: GetAllItemGroup");

            return group;
        }

        public List<SubItemGroup> GetSubGroup(string itemGroupId)
        {
            logger.Info("\tStart Business: Master; Function: GetSubGroup");

            SubItemGroupDataAccess access = new SubItemGroupDataAccess(this.DbContext);
            List<SubItemGroup> subGroup = new List<SubItemGroup>();

            try
            {
                subGroup = access.GetSubGroup(itemGroupId);
            }
            catch (Exception ex)
            {
                logger.Error("\tError Business: Master; Function: GetSubGroup", ex);
                throw ex;
            }

            logger.Info("\tEnd Business: Master; Function: GetSubGroup");

            return subGroup;
        }

        public SubItemGroup GetSubGroupById(string itemGroupId)
        {
            logger.Info("\tStart Business: Master; Function: GetSubGroup");

            SubItemGroupDataAccess access = new SubItemGroupDataAccess(this.DbContext);
            SubItemGroup subGroup = new SubItemGroup();

            try
            {
                subGroup = access.GetSubGroupById(itemGroupId);
            }
            catch (Exception ex)
            {
                logger.Error("\tError Business: Master; Function: GetSubGroup", ex);
                throw ex;
            }

            logger.Info("\tEnd Business: Master; Function: GetSubGroup");

            return subGroup;
        }
    }
}